<?php

/**
 * xdq
 */
class Action
{

    public function index() {

        $module_path = '/module/fengshen';

        Response::assign('module_path', $module_path);
        if (isset($_SESSION['user_id'])) {
            $user_id = $_SESSION['user_id'];
            switch ($_SESSION['user_role_id']) {
                case user_config::AGENCY:
                    $store_list = _model('store')->getList(array('agency_id' => $user_id));
                    break;
                case user_config::MARKETING:
                    $store_list = _model('store')->getList(array('marketing_id' => $user_id));
                    break;
            }

            $user_info = user_helper::get_user_info($user_id);

            $S = array();

            foreach ($store_list as $item) {
                $S[] = array('id' => $item['id'], 'title' => $item['title'] . '-' . $item['title_branch']);
            }

            Response::assign('parent_name', $user_info['realname']);
            Response::assign('parent_id', $user_id);
            Response::assign('store_list', json_encode($S));
            Response::display('task.html');
        } else {
            Response::display('login.html');
        }
    }

    public function logout() {
        if (isset($_SESSION['user_id'])) {
            unset($_SESSION['user_id']);
            unset($_SESSION['user_role_id']);
        }
        Response::redirect('fengshen');
    }

    public function login() {
        $username = Request::Post('username', '');     //手机号
        $password = Request::Post('password', '');     //密码

        // 验证手机号
        if (!(user_helper::is_mobile($username))) {
            return_data(1, 'failed');
        }

        // 验证用户名密码
        $user_id = user_helper::get_user_id_by_password($username, $password);
        if (empty($user_id)) {
            return_data(1, 'failed');
        }
        $userinfo = user_helper::get_user_info($user_id);

        //判断是否为合伙人或市场人员
        if (!(user_config::AGENCY == $userinfo['role_type'] || user_config::MARKETING == $userinfo['role_type'])) {
            return_data(1, 'failed');
        }
        //
        //        //验证密码
        //        if ($userinfo['password'] !== user_helper::md5_password($password, $userinfo['hash'])) {
        //            return_data(1, 'failed');
        //        }

        $_SESSION['user_role_id'] = intval($userinfo['role_type']);
        $_SESSION['user_id'] = $userinfo['id'];
        return_data(0, 'success');

    }


    public function upload() {

        $module_path = '/module/fengshen';
        $success = 0;
        $message = '上传失败, 请稍后重试';

        Response::assign('module_path', $module_path);
        Response::assign('success', 0);

        $parent_id = Request::Post('parent_id', '');    //门店

        if (!isset($_SESSION['user_id']) || $_SESSION['user_id'] !== $parent_id) {
            Response::redirect('fengshen');
            return false;
        }
        $store_id = Request::Post('store_id', 0);    //门店
        // $store_name = Request::Post('store_name', 0);    //门店
        $image = Request::Post('image', '');    //门店对应的图片
        $image_data = Request::Post('image_data', '');

        $extname = pathinfo($image, PATHINFO_EXTENSION);
        if ($extname == 'jpeg') {
            $extname = 'jpg';
        }

        if (!$image || !$image_data || !$store_id) {
            $message = '上传失败, 请确认门店与图片';
        } else {
            $table_name = 'topic_fengshen';
            $file_name = md5($image . datetime()) . '.' . $extname;

            // 图片上传
            file_put_contents('/tmp/' . $file_name, base64_decode($image_data));

            //        $check_result = check_size($file_name, $image);
            //        if (!$check_result['result']) {
            //            return_data(1, '图片超过2M');
            //        }

            if (!ONDEV) {
                $file_path = aliysso_helper::sso_upload_file($file_name, '/tmp/' . $file_name);
            } else {
                $file_path = move_image($file_name);
            }

            if ($file_path) {
                //要插入fengshenpic的数据
                $info = array(
                    'parent_id' => $parent_id,
                    'store_id' => $store_id,
                    'src' => $file_path,
                    'status' => 0,
                    'upload_time' => datetime()   //后加的，新上传的时间也要变
                );

                $list = _model($table_name)->getList(array('store_id' => $store_id, 'parent_id' => $parent_id));

                $id = 0;
                if (empty($list)) {
                    $id = _model($table_name)->create($info);
                    if (!$id) {
                        $message = '上传失败, 请稍后重试';
                    }
                } else {
                    $list = $list[0];
                    if ($list['status'] == 1) {
                        $message = '此图片已审核通过, 不允许再次上传';
                    } else {
                        $id = _model($table_name)->update(array('id' => $list['id']), $info);
                        if (!$id) {
                            $message = '上传失败, 请稍后重试';
                        }
                    }
                }

                if ($id) {
                    $message = '上传成功';
                    $success = 1;
                }
            }
        }

        Response::assign('success', $success);
        Response::assign('message', $message);
        Response::display('result.html');
    }

}


?>